// pages/picktime/picktime.js
var e = getApp(), t = e.requirejs("core"), a = e.requirejs("jquery");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qyitime: '',
    todaytime: [
      { 'data': '', "status": false, "time": "11:00", "mark": "today", "isShow": true },
      { 'data': '', "status": false, "time": "19:00", "mark": "today", "isShow": true }
    ],
    ordertime: [
      { 'data': '', "status": false, "time": "11:00", "mark": "order", "isShow": true },
      { 'data': '', "status": false, "time": "19:00", "mark": "order", "isShow": true },
      { 'data': '', "status": false, "time": "11:00", "mark": "order", "isShow": true },
      { 'data': '', "status": false, "time": "19:00", "mark": "order", "isShow": true }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    var date = new Date();
    var nextDate = new Date(date.getTime() + 24 * 60 * 60 * 1000); 
    
    date.setTime(date.getTime());
    console.log(date, nextDate,7878)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    console.log(year, month, day, hour, minute, second);
    let todaytime = that.data.todaytime;
    let ordertime = that.data.ordertime;
    for (var i = 0; i < todaytime.length; i++) {
      todaytime[i].data = GetDateStr(0);
      var time = Number(todaytime[i].time.slice(0, 2)) * 3600
      if (hour * 3600 >= time) {
        todaytime[i].isShow = false
      }
    }
    for (var i = 0; i < ordertime.length; i++) {
      if (i < 2) {
        ordertime[i].data = GetDateStr(1);
      } else {
        ordertime[i].data = GetDateStr(2);
      }
    }

    that.setData({
      todaytime: todaytime,
      ordertime: ordertime
    })

  },
  dateclick(e) {
    var that = this;
    var mark = e.currentTarget.dataset.mark;
    var index = e.currentTarget.dataset.index;
    var date = e.currentTarget.dataset.date;
    var time = e.currentTarget.dataset.time;
    var todaytime = that.data.todaytime;
    var ordertime = that.data.ordertime;
    if (mark === 'today') {
      for (var i = 0; i < todaytime.length; i++) {
        todaytime[i].status = false
      }
      for (var i = 0; i < ordertime.length; i++) {
        ordertime[i].status = false
      }
      todaytime[index].status = true;
      that.setData({
        todaytime: todaytime,
        ordertime: ordertime
      })
    }
    if (mark === 'order') {
      for (var i = 0; i < todaytime.length; i++) {
        todaytime[i].status = false
      }
      for (var i = 0; i < ordertime.length; i++) {
        ordertime[i].status = false
      }
      ordertime[index].status = true;
      that.setData({
        todaytime: todaytime,
        ordertime: ordertime
      })
    }
    var qyitime = date + ' ' + time;
    that.setData({
      qyitime: date + ' ' +time,
    })

  },
  confirm(res) {
    let _this = this;
    if (_this.data.qyitime == '') {
      e.showError('请先选择时间');
      return false;
    }
    e.setCache("qyitime", _this.data.qyitime, 30), wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function GetDateStr(AddDayCount) {
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期 
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;//获取当前月份的日期 
  var d = dd.getDate();
  return y+"-"+m + "-" + d;
} 